<template>
  <div class="login-container" >
    <h2>登录</h2>
    <el-form ref="loginFormRef" :model="loginForm" class="login-form" :rules="rulesLoginForm" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"
                  prefix-icon="el-icon-lock"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123'
      },
      rulesLoginForm: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {
            pattern: /^[a-zA-Z0-9]+$/, message: '用户名只能包含字母和数字', trigger: 'blur'
          }
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    async login() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      // 表单校验
      this.$refs.loginFormRef.validate(async valid => {
        loading.close();
        if (!valid) {
          return this.$message.error('请输入完整的登录信息');
        }

        // 发送异步请求
        const {data} = await this.$http.post(`/community/employee/Login/`, this.loginForm);
        if (data !== '') {
          // 判断status是否禁用
          if (data.status === 1) {
            this.$message.error('账号已禁用无法登录，请联系管理员！');
            return;
          }
          // 判断isDelete是否已删除
          if (data.isDelete === 1) {
            this.$message.error('账号已无法使用！');
            return;
          }


          // 保存 token 到 sessionStorage
          sessionStorage.setItem('token', data.username);

          // 通过编程式导航将员工信息传递给菜单页
          this.$router.push({
            path: '/home',
            query: {
              employeeId: data.id
            }
          });
        } else {
          this.$message.error('账号密码错误');
        }
      });
    },
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    }
  }
};
</script>
<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  position: relative; /* 使用相对定位 */
  top: 50%; /* 向上移动容器的一半高度 */
  transform: translateY(50%); /* 使用 transform 在垂直方向上居中 */

}


h2 {
  text-align: center;
  margin-bottom: 20px;
}

.el-button--primary {
  background-color: #007bff;
  color: #fff;
}

.el-button--primary:hover {
  background-color: #0056b3;
}

.el-input:hover, .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}

.el-input:focus, .el-textarea:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px #007bff;
  outline: none;
}

.colorful-card {
  background-color: #f4b4cf;
  background-image: linear-gradient(to right, #f4b4cf, #c894db, #7d7bd1, #3da3f5);
  color: #333;
}
</style>